﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ePioneer_web_ep_question_userques : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Users"];
        if (cookie != null)
        {
            if (cookie["userid"] != "")
                Session["userid"] = cookie["userid"];
        }
        if (Session["userid"] == null || Session["userid"].ToString()=="")
        {
            //Response.Write("<script>alert('请先注册登录后再进行提问或查询，谢谢！。'); this.history.back(-1);</script>");
            Response.Write("<script>alert('请先注册并登录后再进行提问或查询，谢谢！。'); window.location.href='ep_service.aspx';</script>");
            Response.End();
        }
        if (!IsPostBack)
        {
            hid_questiontype.Value = string.IsNullOrEmpty(Request["tid"]) ? "1" : Request["tid"];
            bd(1);
        }
      
    }

    private void bd(int page)
    {
        if (Session["userid"] != null)
        {
            labUserName.InnerText=Session["userid"].ToString();
            this.hid_userid.Value = Session["userid"].ToString();
            string strUserid = Session["userid"].ToString();   //"1"; 
            string cmsService = System.Configuration.ConfigurationManager.ConnectionStrings["CMSService"].ToString();
            cmsService += "ep_question.ashx?action=GetQAllQuestionByUserId&UserId=" + strUserid;
            var qlist = UrlGet.GetObject<List<ePioneer_Question>>(cmsService);

            //CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();

            int iUnSolveNum = qlist.Where(x => x.IsClose == 0).Count(); //dal.ePioneer_Question.Where(x => x.UserId == strUserid && x.IsClose == 0).Count();
            int iSolveNum = qlist.Where(x => x.IsClose == 1).Count();// dal.ePioneer_Question.Where(x => x.UserId == strUserid && x.IsClose == 1).Count();

            question_solve_num.InnerHtml = iSolveNum.ToString();
            question_unsolve_num.InnerHtml = iUnSolveNum.ToString();



            int iSolve = -1;   //查处全部，解决和未解决
            if (Request["solve"] != null)
            {
                //传入值为1则为
                iSolve = Convert.ToInt32(Request["solve"]);
                if (iSolve == 0)
                {
                    div_solve.Attributes.Add("class", "user_que");
                    div_unsovle.Attributes.Add("class", "user_sque");
                }
                else
                {
                    div_solve.Attributes.Add("class", "user_sque");
                    div_unsovle.Attributes.Add("class", "user_que");
                }
            }
            else
            {
                div_solve.Attributes.Add("class", "user_que");
                div_unsovle.Attributes.Add("class", "user_que");
            }


            var ep_question_list = qlist.Where(x => x.IsClose != iSolve); //dal.ePioneer_Question.Where(x => x.UserId == strUserid && x.IsClose != iSolve);//.OrderByDescending(x => new { x.IsClose, x.CreateDate });

            AspNetPager1.RecordCount = ep_question_list.Count();
            AspNetPager1.CurrentPageIndex = page;
            ep_question_list = ep_question_list.OrderByDescending(x => x.CreateDate).Skip((page - 1) * AspNetPager1.PageSize).Take(AspNetPager1.PageSize);//.OrderByDescending(x => new { x.IsClose, x.CreateDate });
            foreach (ePioneer_Question epq in ep_question_list)
            {
                epq.Detail = epq.Detail.Length > 10 ? epq.Detail.Substring(0, 10) + "..." : epq.Detail;
            }
            Repeater1.DataSource = ep_question_list;
            Repeater1.DataBind();


        }
        else {
            Response.Redirect("ep_pioneer.html");
        }
    
    }

    public int GetPage()
    {
        return AspNetPager1.CurrentPageIndex;
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bd(AspNetPager1.CurrentPageIndex);
       
    }

    //中文转换
    public string GetIsClose(object iclose)
    {
        if (int.Parse(iclose.ToString())==1)
        {
            return "已解决";
        }
        else
            return "未解决";
    }   
}